#include<iostream>
#include<algorithm>
#include<assert.h>

using namespace std;

template <class T>
class Set
{
private:
    T* _arr;
    int _size;
    int _capacity;
public:
    Set(int len = 50)
        :_arr(new T[len]),
        _size(0),
        _capacity(len) {
    }
    Set(const Set<T>& A)  
    {
        reserve(A.capacity());
        for (int i = 0; i < A.size(); i++)
        {
            _arr[i] = A[i];
        }
        _size = A.size();
        _capacity = A.capacity();
    }
    ~Set()
    {
        delete[] _arr;
        _size = _capacity = 0;
    }

    T* begin()
    {
        return _arr;
    }

    const T* begin() const
    {
        return _arr;
    }

    T* end()
    {
        return _arr + _size;
    }

    const T* end() const
    {
        return _arr + _size;
    }

    void Swap(Set<T>& s)
    {
        std::swap(_arr, s._arr);
        std::swap(_size, s._size);
        std::swap(_capacity, s._capacity);
    }

    void Sort()
    {
        std::sort(begin(), end());
    }

    Set<T>& operator= (Set<T> r)
    {
        Swap(r);
        return *this;
    }

    const T* Find(const T& s) const  
    {
        for (int i = 0; i < _size; i++)
        {
            if (_arr[i] == s)
            {
                return _arr + i;
            }
        }
        return end();
    }

    const T& operator[](int n) const 
    {
        assert(n >= 0 && n < _size);
        return _arr[n];
    }

    Set<T> operator+ (const Set<T>& r) const
    {
        Set<T> tem;
        for (int i = 0; i < _size; i++)
        {
            if (!tem.isExist(_arr[i]))
                tem.push_back(_arr[i]);
        }
        for (int i = 0; i < r.size(); i++)
        {
            if (!tem.isExist(r[i]))
                tem.push_back(r[i]);
        }
        return tem;
    }
    void display() const
    {
        for (int i = 0; i < _size; i++)
        {
            cout << _arr[i] << " ";
        }
        cout << endl;
    }
    bool isExist(const T& v) const
    {
        return Find(v) != end();
    }
    void push_back(const T& v) 
    {
        if (_size == _capacity)
        {
            reserve(2 * _capacity);
        }
        _arr[_size] = v;
        _size++;
    }
    int size() const
    {
        return _size;
    }
    int capacity() const
    {
        return _capacity;
    }
    void Resize(int n, T t = T())
    {
        if (n > _capacity)
        {
            reserve(n);
        }

        if (n > _size)
        {
            for (int i = _size; i < n; i++)
            {
                _arr[i] = t;
            }
            _size = n;
        }
        else if (n < _size)
        {
            _size = n;
        }
    }

    void reserve(int n)
    {
        if (n <= _capacity) return;

        T* newarr = new T[n];
        for (int i = 0; i < _size; i++)
        {
            newarr[i] = _arr[i];
        }
        delete[] _arr;
        _arr = newarr;
        _capacity = n;
    }
};

int main()
{
    Set<int> s1;
    s1.push_back(10);
    s1.push_back(20);
    s1.push_back(30);
    s1.push_back(40);
    s1.push_back(50);

    s1.display();

    Set<int> s2(s1);

    s2.display();

    Set<int> s3;
    s3.Resize(10, 1);

    s3.display();

    Set<int> s4;
    s4.push_back(1);
    s4.push_back(2);
    s4.push_back(3);
    s4.push_back(4);
    s4.push_back(5);
    s4.push_back(6);

    (s4 + s3).display();

    return 0;
}